/**
 * 作者：陈文斌 on 2018/12/28.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'package:flutter/material.dart';
import 'package:crm/constant/constant.dart';
import 'package:crm/viewModel/Login/login_view_model.dart';
import 'package:crm/page/SettingPassword/crm_setting_gesture_password_page.dart';
import 'package:crm/view/AppBar/custom_app_bar.dart';

const String Title = '设置密码';

class SettingTextPasswordPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _SettingTextPasswordPageState();
}

class _SettingTextPasswordPageState extends State<SettingTextPasswordPage> {
  String _firstPass = '';
  String _secondPass = '';

  //创建焦点 用户输入框焦点切换
  FocusNode _secondPassTextFieldNode = new FocusNode();

  @override
  Widget build(BuildContext context) {
    // 输入框管理器
    TextEditingController _firstPassController =
        TextEditingController.fromValue(
      TextEditingValue(
        text: _firstPass,
        selection: TextSelection.fromPosition(
          TextPosition(
              affinity: TextAffinity.downstream, offset: _firstPass.length),
        ),
      ),
    );
    TextEditingController _secondPassController =
        TextEditingController.fromValue(
      TextEditingValue(
        text: _secondPass,
        selection: TextSelection.fromPosition(
          TextPosition(
              affinity: TextAffinity.downstream, offset: _secondPass.length),
        ),
      ),
    );

    Widget _passTextFile({bool isFirst = true}) {
      final String _leading = isFirst ? '密码' : '确认密码';
      return Container(
        width: AppScreen.setWidth(300),
        height: 33,
        margin: EdgeInsets.fromLTRB(34, 30, 41, 0),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(width: 1, color: AppColors.LineColor),
          ),
        ),
        child: Row(
          children: <Widget>[
            Container(
              width: 60,
              child: Text(
                _leading,
                style: TextStyle(
                  color: AppColors.TextColor,
                  fontSize: AppFontSizes.Font15,
                  fontFamily: AppFontFamilies.PingFangSC,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
            Expanded(
              child: TextField(
                controller:
                    isFirst ? _firstPassController : _secondPassController,
                textAlign: TextAlign.center,
                obscureText: true,
                style: TextStyle(
                  color: AppColors.TextColor,
                  fontSize: AppFontSizes.Font15,
                  fontFamily: AppFontFamilies.PingFangSC,
                  fontWeight: FontWeight.w500,
                ),
                decoration: new InputDecoration(
                  contentPadding: EdgeInsets.all(0),
                  hintText: isFirst ? '请输入登录密码' : '请确认登录密码',
                  border: InputBorder.none,
                  hintStyle: TextStyle(
                    color: AppColors.PlaceholderTextColor,
                    fontSize: AppFontSizes.Font15,
                    fontFamily: AppFontFamilies.PingFangSC,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                focusNode: !isFirst ? _secondPassTextFieldNode : null,
                textInputAction:
                    isFirst ? TextInputAction.next : TextInputAction.done,
                onChanged: (str) {
                  if (isFirst) {
                    _firstPass = str;
                  } else {
                    _secondPass = str;
                  }
                  setState(() {});
                },
                onEditingComplete: () {
                  if (isFirst) {
                    FocusScope.of(context)
                        .requestFocus(_secondPassTextFieldNode);
                  } else {
                    FocusScope.of(context).requestFocus(FocusNode());
                  }
                },
              ),
            ),
          ],
        ),
      );
    }

    Widget _buttonSection() {
      Color _defaultColor = Color(0xFF9AD1FF);
      Color _highlightColor = Color(0xFF3C86F3);
      bool _isHighlight = _firstPass.length > 0 && _secondPass.length > 0;

      return Container(
        width: AppScreen.setWidth(300),
        height: 42,
        margin: EdgeInsets.only(
          left: AppScreen.setWidth(36),
          top: AppScreen.setHeight(130),
          right: AppScreen.setWidth(36),
        ),
        decoration: BoxDecoration(
          color: !_isHighlight ? _defaultColor : _highlightColor,
          borderRadius: BorderRadius.circular(21),
        ),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(21),
          child: MaterialButton(
            onPressed: () {
              LoginVM().goSettingPasswordText(
                  context: context,
                  firstPass: _firstPass,
                  secondPass: _secondPass);
            },
            child: Text(
              '下一步',
              style: TextStyle(
                fontSize: AppFontSizes.Font15,
                fontFamily: AppFontFamilies.PingFangSC,
                fontWeight: FontWeight.w500,
              ),
            ),
            textColor: Colors.white,
          ),
        ),
      );
    }

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: CustomAppBar(
        title: Title,
      ),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          FocusScope.of(context).requestFocus(new FocusNode());
        },
        child: Column(
          children: <Widget>[
            _passTextFile(isFirst: true),
            _passTextFile(isFirst: false),
            _buttonSection(),
          ],
        ),
      ),
    );
  }
}
